package com.reps.service;

import org.dom4j.Element;

import java.util.List;

/**
 * @author chl
 * @date 2018/7/17 12:09
 */
public class SetValueTemplate {



    private static List<Element> control_list=null;




    public static String getOneValueByWinId(int winid)
    {
        String searchWinId="dx_"+winid+"_";
        if(control_list!=null)
        {

            for(Element e:control_list)
            {

                if(e.attributeValue("cid")==null)
                    continue;


                if(e.attributeValue("cid").equals(searchWinId))
                {
                    Element temp=(Element) e.elements().get(0);
                    return temp.getText();
                }


            }

        }
        return "";
    }


    public static String getOneValueByWinId3(int winid)
    {
        String searchWinId="dx_"+winid+"_";
        if(control_list!=null)
        {

            for(Element e:control_list)
            {

                if(e.attributeValue("cid")==null)
                    continue;


                if(e.attributeValue("cid").equals(searchWinId))
                {
                   // Element temp=(Element) e.elements().get(0);
                    List<Element> temp=e.elements();
                    if(temp.size()==2)
                    {
                        return temp.get(0).getText()+"^"+temp.get(1).getText();
                    }else
                    {
                        if(temp.get(0).getText().contains("&"))
                            return temp.get(0).getText().split("&")[0];
                        else
                            return temp.get(0).getText();
                    }

                }


            }

        }
        return "";
    }


    public static List<Element> getOneValueByWinId2(int winid)
    {
        String searchWinId="dx_"+winid+"_";
        if(control_list!=null)
        {

            for(Element e:control_list)
            {

                if(e.attributeValue("cid")==null)
                    continue;


                if(e.attributeValue("cid").equals(searchWinId))
                {
                    List<Element> temp= e.elements();
                    return temp;
                }


            }

        }
        return null;
    }



    /**
     *获取选中了第几个单选框
     * @param win_start 开始id，此处是整数
     * @param length 有几个单选框
     * @return
     */
    public static int getSelectedSingleBox(int win_start,int length)
    {

        if(control_list!=null)
        {
            for(int i=0;i<length;i++)
            {
                for(Element e:control_list)
                {
                    if(e.attributeValue("cid")!=null)
                    {
                        if(e.attributeValue("cid").equals("dx_"+(win_start+i)+"_"))
                        {
                            return i;
                        }
                    }
                }
            }

        }
        return 0;
    }



    //多个复选框或者已经知道肯定存在的cid的值
    public static void setValue(String win_value, List<Element> param_control_list, ProcessOneProperty process) throws Exception
    {

        for(Element e:param_control_list)
        {
            if(e.attributeValue("cid")==null)
                break;

            if(e.attributeValue("cid").equals(win_value))
            {
                List<Element> temp=e.elements();
                process.process(temp);
                break;
            }
        }

    }



    public static void setValue(String[] win_value, int length, int index, List<Element> control_list, ProcessOneProperty process) throws Exception
    {
        if(win_value.length==length)
        {
            for(Element e:control_list)
            {
                if(e.attributeValue("cid")==null)
                    break;

                if(e.attributeValue("cid").equals(win_value[1]))
                {
                    List<Element> temp=e.elements();
                    process.process(temp);
                    break;
                }
            }

        }
    }

    public static void setValue(String[] win_value, int length, int index, ProcessOneProperty process) throws Exception
    {
       if(control_list!=null)
           setValue(win_value,length,index,control_list,process);
    }

    public static String getMultiCheckboxValueString(int win_value, int length) throws Exception
    {
        String returnValue="";
        for(int i=0;i<length;i++)
        {

            for(Element e:control_list)
            {
                if(e.attributeValue("cid")==null)
                    continue;

                if(e.attributeValue("cid").trim().equals("dx_"+(win_value+i)+"_"))
                {
                    List<Element> temp=e.elements();
                    if(length>1)
                    {
                        if(temp.get(0).getText().contains("$"))
                            returnValue+=temp.get(0).getText().split("\\$")[0]+",";
                    }else
                    {
                        if(temp.get(0).getText().contains("$"))
                            returnValue+=temp.get(0).getText().split("\\$")[0];
                        else
                            returnValue+=temp.get(0).getText();

                        return returnValue;
                    }

                    break;
                }
            }

        }
        return returnValue;
    }



    public static String getMultiCheckboxValueString2(int win_value, int length) throws Exception
    {
        String returnValue="";
        for(int i=0;i<length;i++)
        {

            for(Element e:control_list)
            {
                if(e.attributeValue("cid")==null)
                    continue;

                if(e.attributeValue("cid").trim().equals("dx_"+(win_value+i)+"_"))
                {
                    List<Element> temp=e.elements();
                    if(temp.size()==2)
                    {
                        if(length>1)
                        {
                            if(temp.get(1).getText().contains("$"))
                                returnValue+=temp.get(1).getText().split("\\$")[0]+",";
                        }else
                        {
                            if(temp.get(1).getText().contains("$"))
                                returnValue+=temp.get(1).getText().split("\\$")[0];
                            else
                                returnValue+=temp.get(1).getText();

                            return returnValue;
                        }
                    }
                    else
                        returnValue+=temp.get(0).getText();

                    break;
                }
            }

        }
        return returnValue;
    }



    public static void setControl_list(List<Element> param_control_list) {
        control_list = param_control_list;
    }


}
